package io.itit.ecp.admin.bootstrap.config;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import icu.mhb.mybatisplus.plugln.injector.JoinDefaultSqlInjector;
import io.itit.ecp.admin.provide.handler.DataRangeHandler;
import io.itit.ecp.admin.provide.properties.DataRangeConfigProperties;
import io.itit.grass.data.permission.starter.properties.DataPermissionConfigProperties;
import io.itit.grass.tenant.starter.properties.TenantConfigProperties;
import io.itit.grass.web.wall.handler.MyDataPermissionHandler;
import io.itit.grass.web.wall.handler.MyMetaObjectHandler;
import io.itit.grass.web.wall.handler.MyTenantHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Configuration
public class MybatisPlusConfig extends JoinDefaultSqlInjector {

    @Resource
    private TenantConfigProperties tenantProperties;
    @Resource
    private DataRangeConfigProperties dataRangeConfigProperties;
    @Resource
    private DataPermissionConfigProperties dataPermissionProperties;


    @Value("${spring.jackson.time-zone}")
    private String timeZone;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        log.info("Jackson配置的时区: {}", timeZone);
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //防止全表更新和删除插件
//        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());

        //数据权限插件
        if (dataPermissionProperties.getEnable() != null && dataPermissionProperties.getEnable()) {
            interceptor.addInnerInterceptor(new DataPermissionInterceptor(new MyDataPermissionHandler(dataPermissionProperties)));
        }

        //数据范围插件 》》 针对能碳平台的数据权限
        if (dataRangeConfigProperties.getEnable() != null && dataRangeConfigProperties.getEnable()) {
            interceptor.addInnerInterceptor(new DataPermissionInterceptor(new DataRangeHandler(dataRangeConfigProperties)));
        }

        //租户插件
        if (tenantProperties.getEnable() != null && tenantProperties.getEnable()) {
            interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new MyTenantHandler(tenantProperties)));
        }

        //分页插件，注意：启用租户，数据权限拦截器必须在分页插件前面
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }

    /**
     * 自动填充创建人/修改人功能
     *
     * @return
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
        return globalConfig;
    }

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        return super.getMethodList(mapperClass, tableInfo);
    }
}
